<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {buildSubFormData, subOnLoad, changeEditor,configSelectData,hasValue} from '../../../static/js/formUtil.js';
	import { confirmModal,errorModal,ignoreHtml,ptFloat,minusF,getDateStr} from '@/static/js/common.js';
	import moment from"@/static/js/moment.js"
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				formCode: "",
				posCacheKey:"hr-zp-trPlan-titleInfo-pos-cache"
			})
		},
		methods:{
			onSelectHopeArriveDate(time){
				let _that = this
				if(_that.$refs.autoform.formdata.finalArriveDate){
					let isBefore = moment(_that.$refs.autoform.formdata.finalArriveDate).isBefore(time)
					let finalArriveTitle = _that.$refs.autoform.getEditorTitle("titleInfo","finalArriveDate")
					let hopeArriveTitle = _that.$refs.autoform.getEditorTitle("titleInfo","hopeArriveDate")
					if(isBefore){
						errorModal(_that,"提示信息",hopeArriveTitle+"必须小于"+finalArriveTitle,()=>{
							_that.$refs.autoform.clear("titleInfo","hopeArriveDate")
						})
					}
				}
				for (var i = 0; i < _that.datas.length; i++) {
				    if (_that.index === i) {
				        continue;
				    }
				    var rowData = _that.datas[i];
				    if (!rowData.posId) {
				        continue;
				    }
				    if (rowData.posId == _that.$refs.autoform.formdata.posId && (rowData.hopeArriveDate == time || rowData.finalArriveDate == time)) {
				        errorModal(_that,"提示信息","同一个岗位不能选择相同到岗时间",()=>{
				        	_that.$refs.autoform.clear("titleInfo","hopeArriveDate")
				        })
				        return true;
				    }
				}
				_that.om.request('trPlan/verifyPosArriveDate', {
					formCode: _that.formCode,
					posId: _that.$refs.autoform.formdata.posId,
					date: time
				}, 'post', true).then(data => {
					if (data.value.succeed) {
						errorModal(_that,"提示信息",data.value.msg,()=>{
							_that.$refs.autoform.clear("titleInfo","hopeArriveDate")
						})
					}
				});
			},
			onSelectFinalArrive(time){
				let _that = this
				if(_that.$refs.autoform.formdata.hopeArriveDate){
					let isBefore = moment(time).isBefore(_that.$refs.autoform.formdata.hopeArriveDate)
					let finalArriveTitle = _that.$refs.autoform.getEditorTitle("titleInfo","finalArriveDate")
					let hopeArriveTitle = _that.$refs.autoform.getEditorTitle("titleInfo","hopeArriveDate")
					if(isBefore){
						errorModal(_that,"提示信息",finalArriveTitle+"必须大于"+hopeArriveTitle,()=>{
							_that.$refs.autoform.clear("titleInfo","finalArriveDate")
						})
					}
				}
				for (var i = 0; i < _that.datas.length; i++) {
				    if (_that.index === i) {
				        continue;
				    }
				    var rowData = _that.datas[i];
				    if (!rowData.posId) {
				        continue;
				    }
				    if (rowData.posId == _that.$refs.autoform.formdata.posId && (rowData.hopeArriveDate == time || rowData.finalArriveDate == time)) {
				        errorModal(_that,"提示信息","同一个岗位不能选择相同到岗时间",()=>{
				        	_that.$refs.autoform.clear("titleInfo","finalArriveDate")
				        })
				        return true;
				    }
				}
				_that.om.request('trPlan/verifyPosArriveDate', {
					formCode: _that.formCode,
					posId: _that.$refs.autoform.formdata.posId,
					date: time
				}, 'post', true).then(data => {
					if (data.value.succeed) {
						errorModal(_that,"提示信息",data.value.msg,()=>{
							_that.$refs.autoform.clear("titleInfo","finalArriveDate")
						})
					}
				});
			},
			clearPos() {
				this.$refs.autoform.clearGroup("titleInfo")
			},
			getSexList(){
				let _that = this
				_that.om.request(
					"dictZero/getListByCode",
					{
						code:"23591"
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("titleInfo","sexCode","data",configSelectData(res,"code","dicname"))
					_that.$refs.autoform.initSelectIndex("titleInfo","sexCode")
				})
			},
			getEduList(){
				let _that = this
				_that.om.request(
					"dictZero/getListByCode",
					{
						code:"23868"
					},
					"post"
				).then(res=>{
					_that.$refs.autoform.changeEditor("titleInfo","eduCode","data",configSelectData(res,"code","dicname"))
					_that.$refs.autoform.initSelectIndex("titleInfo","eduCode")
				})
			},
			
			verifyPosNum(id){
				let _that = this
				_that.om.request(
					"trPlan/verifyPosNum",
					{
						posId: id
					},
					"post"
				).then(res=>{
					if(res.value){ 
						confirmModal(_that,"提示信息",ignoreHtml(res.value),function(){},
						function(){
							_that.$refs.autoform.clear("titleInfo","posId")
							_that.$refs.autoform.clear("titleInfo","instShort")
							_that.$refs.autoform.clear("titleInfo","posGrade")
							_that.$refs.autoform.clear("titleInfo","salaryWay") 
						})
					}
				})
			},
			getData(id){
				let _that = this
				_that.om.request(
					"trPlan/getData",
					{
						posId:id
					},
					"post"
				).then(res=>{
					_that.$refs.autoform.formdata.applyNum = res.value.applyNum
					_that.$refs.autoform.formdata.sexCode = res.value.sexCode
					_that.$refs.autoform.formdata.sexName = res.value.sexName 
					_that.$refs.autoform.initSelectIndex("titleInfo","sexCode")
					_that.$refs.autoform.formdata.eduCode = res.value.eduCode
					_that.$refs.autoform.formdata.eduName = res.value.eduName
					_that.$refs.autoform.initSelectIndex("titleInfo","eduCode")
					_that.$refs.autoform.formdata.posGrade = res.value.posGrade
					_that.$refs.autoform.formdata.major = res.value.major
					_that.$refs.autoform.formdata.workAge = res.value.workAge
					_that.$refs.autoform.formdata.hopeArriveDate = res.value.hopeArriveDate
					_that.$refs.autoform.formdata.finalArriveDate = res.value.finalArriveDate
					_that.$refs.autoform.formdata.remark = res.value.remark
					
				})
			}
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key:_that.posCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.posId = d.posid
							formdata.posName = d.posname
							formdata.instShort = d.instname
							formdata.posGrade = d.careercat
							formdata.salaryWay = d.saraycat
							_that.verifyPosNum(d.posid)
							_that.getData(d.posid)
							uni.removeStorage({
								key:_that.posCacheKey
							})
						}
					}
				})
			}
		},
		onLoad(e) {
			let _that = this
			this.formCode = uni.getStorageSync('formCodeNum')
			subOnLoad(_that, e, function() {
			if(e.configDataCacheKey){
				uni.getStorage({
					key: e.configDataCacheKey,
					success: function(res) {
						if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
							_that.autoformdata = res.data
							Object.assign(_that.initValue, _that.formdata)
							_that.autoformdata[0].order = _that.order
							changeEditor("titleInfo","posId",{
								"link":'/pages/public/pageSearch/pageSearch',
								"linkParam":{
									other:JSON.stringify({}),
									namefield:"posname",
									notefield:"instname",
									searchfield:"q",
									initload:true,
									url:"trPlan/getPosList",
									cachename:_that.posCacheKey,
									placeholder:"请输入关键字"
								}
							},null,_that.autoformdata)
							_that.getEduList()
							_that.getSexList()
							}
						}
					})
				}
							
			})
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                                                  
